/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import worker from '@ohos.worker';
import http from '@ohos.net.http';
import hilog from '@ohos.hilog';

export async function post100kFile() {
  let httpRequest = http.createHttp()
  httpRequest.request(
    "https://www.baidu.com/",
    {
      method: http.RequestMethod.POST, // 可选，默认为http.RequestMethod.GET
      // 开发者根据自身业务需要添加header字段
      header: {
        'Content-Type': 'application/json',
      },
      extraData: {
        "data": "data to send",
      },
      connectTimeout: 60000, // 可选，默认为60s
      readTimeout: 60000, // 可选，默认为60s
    }, (err, data) => {
    if (!err) {
      return "0"
    } else {
      httpRequest.destroy();
      return "0"
    }
  }
  )
  return "0"
}

var parentPort = worker.workerPort; // 获取parentPort属性
// 接收宿主线程的数据
parentPort.onmessage = async function (e) { // 定义onmessage方法
  let data = e.data
  hilog.info(0X0000, "TAOBAO", "tao bao on message Data post100kFile" + JSON.stringify(data))
  const newData = await post100kFile()
  parentPort.postMessage(newData) // 返回值
}